﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestPressure
{
    internal class SqlSugarHelper
    {
        /// <summary>联接数据库
        /// 
        /// </summary>
        static string DBPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "collect.db");

        //static string connectionString = "Data Source=" + DBPath + ";Version=3;";
        static string connectionString = "Data Source=" + DBPath;

        /// <summary>错误代码
        /// 
        /// </summary>
        static int ErrorCode = -1;

        public static SqlSugarScope DB = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = connectionString,//连接符字串
            DbType = DbType.Sqlite,//数据库类型
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute,
        },
            db =>
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
                };
                db.Aop.OnError = (exp) =>//SQL报错
                {
                    Console.WriteLine("SQL执行错误：【" + exp.Message + "】");
                };
            }
         );

        /// <summary>连接状态
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool State()
        {
            return DB.Ado.IsValidConnection();
        }

        /// <summary>插入数据
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Insertable<T>(List<T> list) where T : class, new()
        {
            try
            {
                int res = DB.Insertable(list).ExecuteCommand();
                return res;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("插入记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>删除记录
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int Deleteable<T>(List<T> list) where T : class, new()
        {
            try
            {
                return DB.Deleteable<T>(list).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("删除记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>更新记录
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Updateable<T>(List<T> list) where T : class, new()
        {
            try
            {
                return DB.Updateable<T>(list).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("更新记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>查询符合条件的记录
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        public static List<T> Inquireable<T>(Dictionary<string, Object>? where)
        {
            try
            {
                ISugarQueryable<T>? sugarQueryable = null;
                if (where != null)
                {
                    sugarQueryable = DB.Queryable<T>().WhereColumns(where);
                }
                else
                {
                    sugarQueryable = DB.Queryable<T>();
                }
                List<T> list = sugarQueryable.ToList();
                return list;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("查询记录异常：" + ex.Message);
                return new List<T>();
            }
        }
    }
}

public class DeviceItem : AntdUI.NotifyProperty
{
    public DeviceItem(int deviceNumber, string deviceName, int deviceAddress, string portAddress, int baudRate)
    {
        _deviceNumber = deviceNumber;
        _deviceName = deviceName;
        _deviceAddress = deviceAddress;
        _portAddress = portAddress;
        _baudRate = baudRate;
        _operation = new AntdUI.CellLink[] {
                        //new AntdUI.CellButton("edit") { BorderWidth=1, IconSvg="EditOutlined"},
                        new AntdUI.CellButton("delete") { BorderWidth=1, IconSvg="DeleteOutlined"},
                        new AntdUI.CellButton("point") { BorderWidth=1, IconSvg="ApartmentOutlined"},
        };
    }

    int _deviceNumber;
    public int deviceNumber
    {
        get => _deviceNumber;
        set
        {
            if (_deviceNumber == value) return;
            _deviceNumber = value;
            OnPropertyChanged("deviceNumber");
        }
    }

    string _deviceName;
    public string deviceName
    {
        get => _deviceName;
        set
        {
            if (_deviceName == value) return;
            _deviceName = value;
            OnPropertyChanged("deviceName");
        }
    }

    int _deviceAddress;
    public int deviceAddress
    {
        get => _deviceAddress;
        set
        {
            if (_deviceAddress == value) return;
            _deviceAddress = value;
            OnPropertyChanged("deviceAddress");
        }
    }

    string _portAddress;
    public string portAddress
    {
        get => _portAddress;
        set
        {
            if (_portAddress == value) return;
            _portAddress = value;
            OnPropertyChanged("portAddress");
        }
    }

    int _baudRate;
    public int baudRate
    {
        get => _baudRate;
        set
        {
            if (_baudRate == value) return;
            _baudRate = value;
            OnPropertyChanged("baudRate");
        }
    }

    int _di_point_count;
    public int di_point_count
    {
        get => _di_point_count;
        set
        {
            if (_di_point_count == value) return;
            _di_point_count = value;
            OnPropertyChanged("di_point_count");
        }
    }

    int _do_point_count;
    public int do_point_count
    {
        get => _do_point_count;
        set
        {
            if (_do_point_count == value) return;
            _do_point_count = value;
            OnPropertyChanged("do_point_count");
        }
    }

    int _input_register_count;
    public int input_register_count
    {
        get => _input_register_count;
        set
        {
            if (_input_register_count == value) return;
            _input_register_count = value;
            OnPropertyChanged("input_register_count");
        }
    }

    int _holding_register_count;
    public int holding_register_count
    {
        get => _holding_register_count;
        set
        {
            if (_holding_register_count == value) return;
            _holding_register_count = value;
            OnPropertyChanged("holding_register_count");
        }
    }

    AntdUI.CellLink[] _operation;
    public AntdUI.CellLink[] operation
    {


        get => _operation;
        set
        {
            _operation = value;
            OnPropertyChanged("operation");
        }
    }
}

[SugarTable("c_device")]
public class Device
{
    //数据库是主键需要加上IsPrimaryKey 
    [SugarColumn(IsPrimaryKey = true)]
    public int device_number { get; set; } //设备编号

    public string? device_name { get; set; }//设备名称

    public int device_address { get; set; }//设备地址

    public string? port_address { get; set; }//串口地址

    public int baudrate { get; set; }//波特率

    public int di_point_count { get; set; }//DI寄存器数量

    public int do_point_count { get; set; }//DO寄存器数量

    public int input_register_count { get; set; }//只读寄存器数量

    public int holding_register_count { get; set; }//保持寄存器数量
}

public class DevicePointItem : AntdUI.NotifyProperty
{
    public DevicePointItem(int deviceNumber, string? deviceName,
                           int pointNumber, string? pointName,
                           int pointAddress, string? range,
                           string? outValue, float amend,
                           string? alarm, string? errorRange,
                           string? unit, string? registerType
                           )
    {
        _device_number = deviceNumber;
        _device_name = deviceName;
        _point_number = pointNumber;
        _point_name = pointName;
        _point_address = pointAddress;
        _register_type = registerType;
        if (_register_type == "InputRegister" || _register_type == "HoldingRegister")
        {
            _range = range;
            _outValue = outValue;
            _alarm = alarm;
            _error_range = errorRange;
            _unit = unit;
        }
        _operation = new AntdUI.CellLink[] {
                        //new AntdUI.CellButton("edit") { BorderWidth=1, IconSvg="EditOutlined"},
                        new AntdUI.CellButton("delete") { BorderWidth=1, IconSvg="DeleteOutlined"},
        };
    }

    int _device_number;
    public int device_number
    {
        get => _device_number;
        set
        {
            if (_device_number == value) return;
            _device_number = value;
            OnPropertyChanged("device_number");
        }
    }

    string? _device_name;
    public string? device_name
    {
        get => _device_name;
        set
        {
            if (_device_name == value) return;
            _device_name = value;
            OnPropertyChanged("device_name");
        }
    }

    int _device_address;
    public int device_address
    {
        get => _device_address;
        set
        {
            if (_device_address == value) return;
            _device_address = value;
            OnPropertyChanged("device_address");
        }
    }

    int _point_number;
    public int point_number
    {
        get => _point_number;
        set
        {
            if (_point_number == value) return;
            _point_number = value;
            OnPropertyChanged("point_number");
        }
    }

    string? _point_name;
    public string? point_name
    {
        get => _point_name;
        set
        {
            if (_point_name == value) return;
            _point_name = value;
            OnPropertyChanged("point_name");
        }
    }

    int _point_address;
    public int point_address
    {
        get => _point_address;
        set
        {
            if (_point_address == value) return;
            _point_address = value;
            OnPropertyChanged("point_address");
        }
    }

    string? _range;
    public string? range
    {
        get => _range;
        set
        {
            if (_range == value) return;
            _range = value;
            OnPropertyChanged("range");
        }
    }

    string? _outValue;
    public string? outValue
    {
        get => _outValue;
        set
        {
            if (_outValue == value) return;
            _outValue = value;
            OnPropertyChanged("outValue");
        }
    }

    float _amend;
    public float amend
    {
        get => _amend;
        set
        {
            if (_amend == value) return;
            _amend = value;
            OnPropertyChanged("amend");
        }
    }

    string? _alarm;
    public string? alarm
    {
        get => _alarm;
        set
        {
            if (_alarm == value) return;
            _alarm = value;
            OnPropertyChanged("alarm");
        }
    }

    string? _error_range;
    public string? error_range
    {
        get => _error_range;
        set
        {
            if (_error_range == value) return;
            _error_range = value;
            OnPropertyChanged("error_range");
        }
    }

    string? _unit;
    public string? unit
    {
        get => _unit;
        set
        {
            if (_unit == value) return;
            _unit = value;
            OnPropertyChanged("unit");
        }
    }

    string? _register_type;
    public string? register_type
    {
        get => _register_type;
        set
        {
            if (_register_type == value) return;
            _register_type = value;
            OnPropertyChanged("register_type");
        }
    }

    AntdUI.CellLink[] _operation;
    public AntdUI.CellLink[] operation
    {
        get => _operation;
        set
        {
            _operation = value;
            OnPropertyChanged("operation");
        }
    }
}

[SugarTable("c_device_point")]
public class DevicePoint
{
    //数据库是主键需要加上IsPrimaryKey 
    [SugarColumn(IsPrimaryKey = true)]
    public int point_number { get; set; }//设备点位编号

    public int device_number { get; set; }//设备编号

    public string? device_name { get; set; }//设备名称

    public int device_address { get; set; }//设备地址

    public string? point_name { get; set; }//设备点位名称

    public int point_address { get; set; }//设备点位地址

    public float range_min { get; set; }//设备点位最小量程

    public float range_max { get; set; }//设备点位最大量程

    public float out_min { get; set; }//设备点位最小输出

    public float out_max { get; set; }//设备点位最大输出

    public float amend { get; set; }//设备点位修正值

    public float alarm_hight { get; set; }//设备点位报警最高值

    public float alarm_low { get; set; }//设备点位报警最低值

    public float error_range { get; set; }//设备点位误差范围

    public string? unit { get; set; }//设备点位单位

    public int register_type { get; set; }//设备点位类型 1->DI 2->DO 3->Input 4->Holding
}

public class TemplateItem : AntdUI.NotifyProperty
{

    public TemplateItem(int templateNumber, string pipePosition,
                           float designPressure,
                           string firstStage,
                           string secondStage,
                           string thirdStage,
                           string rigorStage,
                           string intensityStage,
                           string editEnable
                           )
    {
        _template_number = templateNumber;
        _pipe_position = pipePosition;
        _design_pressure = designPressure;
        if (firstStage.Length > 0)
        {
            _first_stage = firstStage;
        }
        if (secondStage.Length > 0)
        {
            _second_stage = secondStage;
        }
        if (thirdStage.Length > 0)
        {
            _third_stage = thirdStage;
        }
        if (rigorStage.Length > 0)
        {
            _rigor_stage = rigorStage;
        }
        if (intensityStage.Length > 0)
        {
            _intensity_stage = intensityStage;
        }
        _edit_enable = editEnable;
        _operation = new AntdUI.CellLink[] {
                       // new AntdUI.CellButton("edit") { BorderWidth=1, IconSvg="EditOutlined"},
                        new AntdUI.CellButton("delete") { BorderWidth=1, IconSvg="DeleteOutlined"},
        };
    }
    int _template_number;
    public int template_number
    {
        get => _template_number;
        set
        {
            if (_template_number == value) return;
            _template_number = value;
            OnPropertyChanged("template_number");
        }
    }

    string? _pipe_position;
    public string? pipe_position
    {
        get => _pipe_position;
        set
        {
            if (_pipe_position == value) return;
            _pipe_position = value;
            OnPropertyChanged("pipe_position");
        }
    }


    float _design_pressure;
    public float design_pressure
    {
        get => _design_pressure;
        set
        {
            if (_design_pressure == value) return;
            _design_pressure = value;
            OnPropertyChanged("design_pressure");
        }
    }

    string? _edit_enable;
    public string? edit_enable
    {
        get => _edit_enable;
        set
        {
            if (_edit_enable == value) return;
            _edit_enable = value;
            OnPropertyChanged("edit_enable");
        }
    }

    string? _first_stage;
    public string? first_stage
    {
        get => _first_stage;
        set
        {
            if (_first_stage == value) return;
            _first_stage = value;
            OnPropertyChanged("first_stage");
        }
    }

    string? _second_stage;
    public string? second_stage
    {
        get => _second_stage;
        set
        {
            if (_second_stage == value) return;
            _second_stage = value;
            OnPropertyChanged("second_stage");
        }
    }

    string? _third_stage;
    public string? third_stage
    {
        get => _third_stage;
        set
        {
            if (_third_stage == value) return;
            _third_stage = value;
            OnPropertyChanged("third_stage");
        }
    }

    string? _rigor_stage;
    public string? rigor_stage
    {
        get => _rigor_stage;
        set
        {
            if (_rigor_stage == value) return;
            _rigor_stage = value;
            OnPropertyChanged("rigor_stage");
        }
    }

    string? _intensity_stage;
    public string? intensity_stage
    {
        get => _intensity_stage;
        set
        {
            if (_intensity_stage == value) return;
            _intensity_stage = value;
            OnPropertyChanged("intensity_stage");
        }
    }

    AntdUI.CellLink[] _operation;
    public AntdUI.CellLink[] operation
    {
        get => _operation;
        set
        {
            _operation = value;
            OnPropertyChanged("operation");
        }
    }
}

[SugarTable("c_template")]
public class Template
{
    //数据库是主键需要加上IsPrimaryKey 
    [SugarColumn(IsPrimaryKey = true)]
    public int template_number { get; set; }//模板编号

    public string? pipe_position { get; set; }//管道位置

    public float design_pressure { get; set; }//设计压力

    public int edit_enable { get; set; }//允许编辑

    public float first_stage_pressure { get; set; }//一阶段压力
    public float first_stage_minute { get; set; }//持续时间
    public int first_stage_estimate { get; set; }//判断方式
    public float first_stage_estimate_value { get; set; }//判断标准

    public float second_stage_pressure { get; set; }//二阶段压力
    public float second_stage_minute { get; set; }//持续时间
    public int second_stage_estimate { get; set; }//判断方式
    public float second_stage_estimate_value { get; set; }//判断标准

    public float third_stage_pressure { get; set; }//三阶段压力
    public float third_stage_minute { get; set; }//持续时间
    public int third_stage_estimate { get; set; }//判断方式
    public float third_stage_estimate_value { get; set; }//判断标准

    public float rigor_stage_pressure { get; set; }//严密段压力
    public float rigor_stage_minute { get; set; }//持续时间
    public int rigor_stage_estimate { get; set; }//判断方式
    public float rigor_stage_estimate_value { get; set; }//判断标准

    public float intensity_stage_pressure { get; set; }//强度段压力
    public float intensity_stage_minute { get; set; }//持续时间
    public int intensity_stage_estimate { get; set; }//判断方式
    public float intensity_stage_estimate_value { get; set; }//判断标准
}

public class ProjectItem : AntdUI.NotifyProperty
{

    public ProjectItem(string? projectNumber, string? projectName,
                           string? stakeMark,
                           string? pipeSpecification,
                           float pipeLength,
                           string? medium,
                           float designPressure,
                           string? pipeNature,
                           string? testType,
                           string? company,
                           string? manometerPrecision,
                           string? manometerRange,
                           int templateId
                           )
    {
        _project_number = projectNumber;
        _project_name = projectName;
        _stake_mark = stakeMark;
        _pipe_specification = pipeSpecification;
        _pipe_length = pipeLength;
        _medium = medium;
        _design_pressure = designPressure;
        _pipe_nature = pipeNature;
        _test_type = testType;
        _company = company;
        _manometer_precision = manometerPrecision;
        _manometer_range = manometerRange;
        _template_id = templateId;
        _operation = new AntdUI.CellLink[] {
                        //new AntdUI.CellButton("edit") { BorderWidth=1, IconSvg="EditOutlined"},
                        new AntdUI.CellButton("delete") { BorderWidth=1, IconSvg="DeleteOutlined"} };
    }

    string? _project_number;
    public string? project_number
    {
        get => _project_number;
        set
        {
            if (_project_number == value) return;
            _project_number = value;
            OnPropertyChanged("project_number");
        }
    }

    string? _project_name;
    public string? project_name
    {
        get => _project_name;
        set
        {
            if (_project_name == value) return;
            _project_name = value;
            OnPropertyChanged("project_name");
        }
    }

    string? _stake_mark;
    public string? stake_mark
    {
        get => _stake_mark;
        set
        {
            if (_stake_mark == value) return;
            _stake_mark = value;
            OnPropertyChanged("stake_mark");
        }
    }

    string? _pipe_specification;
    public string? pipe_specification
    {
        get => _pipe_specification;
        set
        {
            if (_pipe_specification == value) return;
            _pipe_specification = value;
            OnPropertyChanged("pipe_specification");
        }
    }

    float _pipe_length;
    public float pipe_length
    {
        get => _pipe_length;
        set
        {
            if (_pipe_length == value) return;
            _pipe_length = value;
            OnPropertyChanged("pipe_length");
        }
    }

    string? _medium;
    public string? medium
    {
        get => _medium;
        set
        {
            if (_medium == value) return;
            _medium = value;
            OnPropertyChanged("medium");
        }
    }

    float _design_pressure;
    public float design_pressure
    {
        get => _design_pressure;
        set
        {
            if (_design_pressure == value) return;
            _design_pressure = value;
            OnPropertyChanged("design_pressure");
        }
    }

    string? _pipe_nature;
    public string? pipe_nature
    {
        get => _pipe_nature;
        set
        {
            if (_pipe_nature == value) return;
            _pipe_nature = value;
            OnPropertyChanged("pipe_nature");
        }
    }

    string? _test_type;
    public string? test_type
    {
        get => _test_type;
        set
        {
            if (_test_type == value) return;
            _test_type = value;
            OnPropertyChanged("test_type");
        }
    }

    string? _company;
    public string? company
    {
        get => _company;
        set
        {
            if (_company == value) return;
            _company = value;
            OnPropertyChanged("company");
        }
    }

    string? _manometer_precision;
    public string? manometer_precision
    {
        get => _manometer_precision;
        set
        {
            if (_manometer_precision == value) return;
            _manometer_precision = value;
            OnPropertyChanged("manometer_precision");
        }
    }

    string? _manometer_range;
    public string? manometer_range
    {
        get => _manometer_range;
        set
        {
            if (_manometer_range == value) return;
            _manometer_range = value;
            OnPropertyChanged("manometer_range");
        }
    }

    int _template_id;
    public int template_id
    {
        get => _template_id;
        set
        {
            if (_template_id == value) return;
            _template_id = value;
            OnPropertyChanged("template_id");
        }
    }

    AntdUI.CellLink[] _operation;
    public AntdUI.CellLink[] operation
    {
        get => _operation;
        set
        {
            _operation = value;
            OnPropertyChanged("operation");
        }
    }
}

[SugarTable("c_project")]
public class Project
{
    //数据库是主键需要加上IsPrimaryKey 
    [SugarColumn(IsPrimaryKey = true)]
    public string? project_number { get; set; }//工程编号
    public string? project_name { get; set; }//工程名称
    public string? stake_mark { get; set; }//工程桩号
    public string? pipe_specification { get; set; }//管材规格
    public float pipe_length { get; set; }//管道长度
    public string? medium { get; set; }//试压介质
    public float design_pressure { get; set; }//设计压力
    public string? pipe_nature { get; set; }//管道性质
    public string? test_type { get; set; }//试压类型
    public string? company { get; set; }//施工单位
    public string? manometer_precision { get; set; }//压力表精度
    public string? manometer_range { get; set; }//压力表量程
    public int template_id { get; set; }//模板ID
}

[SugarTable("c_form_point")]
public class FormPoint
{
    //数据库是主键需要加上IsPrimaryKey 
    [SugarColumn(IsPrimaryKey = true)]
    public int form_point_number { get; set; }
    public string? form_point_name { get; set; }
    public int device_point_number { get; set; }
    public string? device_point_name { get; set; }
}
public class FormPointItem : AntdUI.NotifyProperty
{

    public FormPointItem(int formPointNumber,
                           string? formPointName,
                           int devicePointNumber,
                           string? devicePointName
                           )
    {
        _form_point_number = formPointNumber;
        _form_point_name = formPointName;
        _device_point_number = devicePointNumber;
        _device_point_name = devicePointName;
        _operation = new AntdUI.CellLink[] {
                        //new AntdUI.CellButton("edit") { BorderWidth=1, IconSvg="EditOutlined"},
                        new AntdUI.CellButton("delete") { BorderWidth=1, IconSvg="DeleteOutlined"} };

    }

    int _form_point_number;
    public int form_point_number
    {
        get => _form_point_number;
        set
        {
            if (_form_point_number == value) return;
            _form_point_number = value;
            OnPropertyChanged("form_point_number");
        }
    }

    string? _form_point_name;
    public string? form_point_name
    {
        get => _form_point_name;
        set
        {
            if (_form_point_name == value) return;
            _form_point_name = value;
            OnPropertyChanged("form_point_name");
        }
    }

    int _device_point_number;
    public int device_point_number
    {
        get => _device_point_number;
        set
        {
            if (_device_point_number == value) return;
            _device_point_number = value;
            OnPropertyChanged("device_point_number");
        }
    }

    string? _device_point_name;
    public string? device_point_name
    {
        get => _device_point_name;
        set
        {
            if (_device_point_name == value) return;
            _device_point_name = value;
            OnPropertyChanged("device_point_name");
        }
    }

    AntdUI.CellLink[] _operation;
    public AntdUI.CellLink[] operation
    {
        get => _operation;
        set
        {
            _operation = value;
            OnPropertyChanged("operation");
        }
    }
}